using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DreamBuilder.UI.UserControls.DataAccess;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.DictionaryAccess;


namespace DreamBuilder.UI.UserControls
{
    public class RefTemplateColumn : IChildItem
    {
        private UserControl userControl = null;
        private ChildTableItemInfo childTableItemInfo;
        RefEditor refEditor = null;
        public event ItemSelectedEventHandler ItemSelected;

        public RefTemplateColumn()
        {

        }

        public RefTemplateColumn(ChildArgs childArgs)
        {
            this.userControl = childArgs.Container;
            this.childTableItemInfo = childArgs.CChildTableItemInfo;
        }
        public void SetControlID()
        {
            refEditor.ID = "ref_" + this.childTableItemInfo.FieldCode;
        }
        public void SetControlAttributes()
        {
            refEditor.TableCode = UChildCommon.GetChildReferenceObj(childTableItemInfo);
            refEditor.MultiSelect = 1;
            refEditor.Width = Unit.Percentage(100);
            refEditor.AutoPostBack = childTableItemInfo.AutoPostBack;
            refEditor.ReadOnly = true;
            refEditor.Enabled = !childTableItemInfo.ReadOnly;
            if (childTableItemInfo.Nullable == false && childTableItemInfo.Show == true)
            {
                refEditor.Required = true;
            }
            if (childTableItemInfo.AutoPostBack == true)
            {
                refEditor.RefSelected += new RefSelectHandler(pRefEditor_RefSelected);
            }
            if (!string.IsNullOrEmpty(childTableItemInfo.SelectRangeSQL))
            {
                refEditor.WhereSql = refEditor.WhereSql == "" ? childTableItemInfo.SelectRangeSQL : ("(" + childTableItemInfo.SelectRangeSQL + ") and (" + refEditor.WhereSql + ")");
            }
        }

        void pRefEditor_RefSelected(object sender, EventArgs e)
        {
            RefEditor currRefEditor = (RefEditor)sender;
            GridViewRow gvRow = currRefEditor.NamingContainer as GridViewRow;
            GridView gvChild = gvRow.NamingContainer as GridView;
            UCChild ucChild = gvChild.Parent as UCChild;
            DataTable curentData = ucChild.OutPutDataTable;
            int index = int.Parse(currRefEditor.Attributes["RowIndex"]);
            object key = gvChild.DataKeys[index][0];
            DataRow row = curentData.Rows.Find(key);
            row[currRefEditor.Attributes["FieldCode"].ToString()] = currRefEditor.RefCodeValue;
            ucChild.Refresh(ref curentData);
            ChildItemEventArgs childItemEventArgs = new ChildItemEventArgs();
            childItemEventArgs.CurrentEventItem = gvChild.Rows[index];
            childItemEventArgs.CurrentEventID = currRefEditor.ID;
            if (ItemSelected != null)
            {
                ItemSelected(sender, childItemEventArgs);
            }
        }

        public void SetControlValue(GridViewRow gvRow, string controlID, FieldDicInfo fieldDicInfo, DataRow dr)
        {

            RefEditor currRefEditor = gvRow.FindControl(controlID) as RefEditor;
            if (currRefEditor != null)
            {
                currRefEditor.Attributes.Add("RowIndex", gvRow.RowIndex.ToString());
                currRefEditor.Attributes.Add("FieldCode", fieldDicInfo.FieldCode);
                currRefEditor.Refresh();
                currRefEditor.RefNameValue = dr[fieldDicInfo.TableCode + "__" + fieldDicInfo.FieldCode].ToString();
                currRefEditor.RefCodeValue = dr[fieldDicInfo.FieldCode].ToString();

            }
           
        }

        public void GetControlValue(GridViewRow gvRow, DataRow dr, string controlID, ChildTableItemInfo childTableItemInfo)
        {
            RefEditor currRefEditor = gvRow.FindControl(controlID) as RefEditor;
            if (currRefEditor != null)
                dr[childTableItemInfo.FieldCode] = currRefEditor.RefCodeValue.Length == 0 ? System.DBNull.Value : (object)currRefEditor.RefCodeValue;
            ChildEditTotalInfo childEditTotalInfo = DictionaryAccessFacade.GetChildEditTotalInfo(childTableItemInfo.ChildEditID);
            FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(childEditTotalInfo.TableCode, childTableItemInfo.FieldCode);
            dr[fieldDicInfo.TableCode + "__" + fieldDicInfo.FieldCode] = currRefEditor.RefNameValue;
        }

        public void InstantiateIn(Control container)
        {
            refEditor = (RefEditor)userControl.LoadControl("RefEditor.ascx");
            SetControlID();
            SetControlAttributes();
            container.Controls.Add(refEditor);
        }
    }
}